package com.jctrip.hotel.source.controller;

import java.util.List;

import javax.annotation.Resource;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.google.common.collect.Lists;
import com.jctrip.hotel.common.util.RandomUtil;
import com.jctrip.hotel.source.model.Result;
import com.jctrip.hotel.source.model.rezen.req.*;
import com.jctrip.hotel.source.model.rezen.resp.*;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import com.jctrip.hotel.source.model.rezen.resp.CityInfoDTO.CityInfoData;
import com.jctrip.hotel.source.model.rezen.resp.RoomTypeDTO.RoomType;
import com.jctrip.hotel.source.model.wehotel.req.OrderData;
import com.jctrip.hotel.source.service.rezen.RezenService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author henry
 * @Date 2023/8/12 10:49
 * @Description
 */
@Slf4j
@Api(tags = "rezen")
@RestController
@RequestMapping("/rezen")
@ApiSupport(order = 9)
public class RezenController {
    @Resource
    private RezenService rezenService;

    @RequestMapping(value = "/getCityData", method = RequestMethod.POST)
    @ApiOperation(value = "getCityData", notes = "城市数据")
    public Result getCityData(@RequestParam String secretId, @RequestParam String secretKey,
                              @RequestParam Long groupId) {
        List<CityInfoData> result;
        try {
            result = rezenService.getCity(secretId, secretKey, groupId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/queryHotelList", method = RequestMethod.POST)
    @ApiOperation(value = "queryHotelList", notes = "酒店列表")
    public Result queryHotelList(@RequestParam String secretId, @RequestParam String secretKey,
                                 @RequestParam Long groupId, @RequestParam String cityCode) {
        List<HotelInfoDTO> result;
        try {
            result = rezenService.queryHotelList(secretId, secretKey, groupId, cityCode);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/queryHotelList2", method = RequestMethod.POST)
    @ApiOperation(value = "酒店列表", notes = "酒店列表")
    public Result queryHotelList2(@RequestParam String secretId, @RequestParam String secretKey,
                                  @RequestParam Long groupId) {
        List<HotelInfoIdDTO> result;
        try {
            result = rezenService.queryHotelList2(secretId, secretKey, groupId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/queryHotelList3", method = RequestMethod.POST)
    @ApiOperation(value = "酒店列表(三方)", notes = "酒店列表(三方)")
    public Result queryHotelListForCompany(@RequestParam String secretId, @RequestParam String secretKey,
                                           @RequestParam Long groupId) {
        List<HotelInfoIdDTO> result;
        try {
            result = rezenService.queryHotelListForCompany(secretId, secretKey, groupId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/queryHotelDetails", method = RequestMethod.POST)
    @ApiOperation(value = "queryHotelDetails", notes = "获取酒店详情")
    public Result queryHotelDetails(@RequestBody HotelDetailsReq hotelDetailsReq,
                                    @RequestParam String secretId,
                                    @RequestParam String secretKey) {
        HotelDetailsResp result;
        try {
            result = rezenService.queryHotelDetails(hotelDetailsReq, secretId, secretKey);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/getCompanyList", method = RequestMethod.POST)
    @ApiOperation(value = "查询公司列表(三方)", notes = "查询公司列表(三方)")
    public Result getCompanyList(@RequestBody HotelListReq req,
                                 @RequestParam String secretId,
                                 @RequestParam String secretKey) {
        List<GetCompanyListResp> result;
        try {
            result = rezenService.getCompanyList(req, secretId, secretKey);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/queryRoomTypeByCompanyId", method = RequestMethod.POST)
    @ApiOperation(value = "查询房型(三方)", notes = "查询房型(三方)")
    public Result queryRoomTypeByCompanyId(@RequestBody ProductBriefListByCompanyIdReq req,
                                           @RequestParam String secretId,
                                           @RequestParam String secretKey) {
        List<ProductBriefListByCompanyIdResp> result;
        try {
            result = rezenService.queryRoomTypeByCompanyId(req, secretId, secretKey);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/getRoomStatusByCompanyId", method = RequestMethod.POST)
    @ApiOperation(value = "查询房态(三方)", notes = "查询房态(三方)")
    public Result getRoomStatusByCompanyId(@RequestBody ProductListByCompanyIdReq req,
                                           @RequestParam String secretId,
                                           @RequestParam String secretKey) {
        List<ProductListByCompanyIdResp> result;
        try {
            result = rezenService.getRoomStatusByCompanyId(req, secretId, secretKey);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/checkPriceAndCount", method = RequestMethod.POST)
    @ApiOperation(value = "下单前校验房态", notes = "检查库存和价格")
    public Result checkPriceAndCount(@RequestBody PostOrderCheckReq req,
                                     @RequestParam String secretId,
                                     @RequestParam String secretKey) {
        List<PostOrderCheckDTO> result;
        try {
            result = rezenService.checkPriceAndCount(req, secretId, secretKey).getBody();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/queryRoomType", method = RequestMethod.POST)
    @ApiOperation(value = "queryRoomType", notes = "查询房型")
    public Result queryRoomType(@RequestParam String secretId, @RequestParam String secretKey,
                                @RequestParam Long groupId, @RequestParam Long hotelId) {
        List<RoomType> result;
        try {
            result = rezenService.queryRoomType(secretId, secretKey, groupId, hotelId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/getRoomStatus", method = RequestMethod.POST)
    @ApiOperation(value = "getRoomStatus", notes = "查询房型")
    public Result getRoomStatus(@RequestParam String secretId, @RequestParam String secretKey,
                                @RequestParam Long groupId,
                                @RequestParam Long hotelId, @RequestParam(required = false) String roomTypeId,
                                @RequestParam String start, @RequestParam String end) {
        ProductListDTO result;
        try {
            result = rezenService.getRoomStatus(secretId, secretKey, groupId, hotelId, roomTypeId, start, end);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @RequestMapping(value = "/reserveOrder", method = RequestMethod.POST)
    @ApiOperation(value = "下单", notes = "下订单")
    public Result reserveOrder(@RequestParam String secretId, @RequestParam String secretKey,
                               @RequestBody RezenPostOrderReq req) {
        RezenOrderResp result;
        try {
            result = rezenService.reserveOrder(secretId, secretKey, req).getBody();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }


    @RequestMapping(value = "/getOrderStatus", method = RequestMethod.POST)
    @ApiOperation(value = "getOrderStatus", notes = "查订单状态")
    public Result getOrderStatus(@RequestParam String secretId, @RequestParam String secretKey,
                                 @RequestParam Long groupId,
                                 @RequestParam String outOrderId) {
        OrderData result;
        try {
            result = rezenService.getOrderStatus(secretId, secretKey, groupId, outOrderId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }


    @RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
    @ApiOperation(value = "cancelOrder", notes = "取消订单")
    public Result cancelOrder(@RequestParam String secretId, @RequestParam String secretKey,
                              @RequestParam Long groupId,
                              @RequestParam String outOrderId,
                              @RequestParam Long orderId) {
        Boolean result;
        try {
            result = rezenService.cancelOrder(secretId, secretKey, groupId, orderId, outOrderId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }


    //================接口直连================
    @RequestMapping(value = "/pullOrderInhotelInfo", method = RequestMethod.GET)
    @ApiOperation(value = "pullOrderInhotelInfo", notes = "接口直连查询订单详情")
    public Result pullOrderInhotelInfo(@RequestParam(value = "sourceId") @ApiParam("供应商账户id") String sourceId,
                                       @RequestParam(value = "orderID") @ApiParam("分销商订单号") String orderID) {

        RezenPullOrderInhotelInfoResp result;
        try {
            RezenPullOrderInhotelInfoReq req = new RezenPullOrderInhotelInfoReq();
            req.setDistributorResIds(Lists.newArrayList(orderID.trim()));
            req.setGroupId(999999999);
            req.setRequestId(RandomUtil.createRandomStr(8));
            result = rezenService.pullOrderInhotelInfo(sourceId, req);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }

        return Result.ok(result);
    }
}
